import CryptoJS from "crypto-js"
import JSEncrypt from "jsencrypt"

// 全局状态globalData
import { storeToRefs } from "pinia";
import { useGlobalDataStore } from '/globalData.js';

/**
 * 加密工具类
 */
const cryptoUtils = {
	/**
	 * AES加密
	 * @param {string|object} words - 需要加密的内容
	 * @param {string} AES_KEY - AES密钥(16位)
	 * @param {string} AES_IV - 偏移量(16位)
	 * @returns {string} 加密后的字符串
	 */
	AES_Encrypt(words, AES_KEY, AES_IV) {
		if (!AES_KEY || AES_KEY.length!== 16) {
			console.error('AES密钥长度不符合要求，必须为16');
			return;
		}
		if (!AES_IV || AES_IV.length!== 16) {
			console.error('偏移量IV长度不符合要求，必须为16');
			return;
		}

		let dataToEncrypt = words;
		if (typeof words === 'object') { // 对象加密
			dataToEncrypt = JSON.stringify(words);
		}

		try{
			const UTF8AES_KEY = CryptoJS.enc.Utf8.parse(AES_KEY);
			const UTF8IV = CryptoJS.enc.Utf8.parse(AES_IV);

			const AESEncrypted = CryptoJS.AES.encrypt(
				CryptoJS.enc.Utf8.parse(dataToEncrypt),
				UTF8AES_KEY,
				{
					iv: UTF8IV,
					mode: CryptoJS.mode.CBC,
					padding: CryptoJS.pad.Pkcs7,
				},
			);
			return AESEncrypted.ciphertext.toString().toUpperCase();
		} catch (err) {
			console.error("AES:2 加密失败", err);
		}
	},

	/**
	 * 使用全局配置进行AES加密
	 * @param {object} words - 需要加密的内容
	 * @returns {string} 加密后的字符串
	 */
	AESEncrypt(words) {
		// 全局状态globalData
		const { globalData } = storeToRefs(useGlobalDataStore())

		const ts = Date.now();
		const TsAndData = { ts, ...words };

		const encrypted_pwd_info = globalData.value.ENCRYPTED_PWD_INFO;
		const aes_key = encrypted_pwd_info.AES_KEY;
		const aes_iv = encrypted_pwd_info.AES_IV;

		return this.AES_Encrypt(TsAndData, aes_key, aes_iv);
	},


	/**
	 * AES解密
	 * @param {string} words - 需要解密的内容
	 * @param {string} AES_KEY - AES密钥(16位)
	 * @param {string} AES_IV - 偏移量(16位)
	 * @returns {string} 解密后的字符串
	 */
	AES_Decrypt(words, AES_KEY, AES_IV) {
		if (!AES_KEY || AES_KEY.length!== 16) {
			console.error('AES密钥长度不符合要求，必须为16');
			return;
		}
		if (!AES_IV || AES_IV.length!== 16) {
			console.error('偏移量IV长度不符合要求，必须为16');
			return;
		}

		try {
			const UTF8AES_KEY = CryptoJS.enc.Utf8.parse(AES_KEY);
			const UTF8IV = CryptoJS.enc.Utf8.parse(AES_IV);
			const Hexwords = CryptoJS.enc.Hex.parse(words);
			const Base64Hexwords = CryptoJS.enc.Base64.stringify(Hexwords);
			const DecryptData = CryptoJS.AES.decrypt(
				Base64Hexwords,
				UTF8AES_KEY,
				{
					iv: UTF8IV,
					mode:CryptoJS.mode.CBC,
					padding:CryptoJS.pad.Pkcs7,
				}
			);
			return DecryptData.toString(CryptoJS.enc.Utf8).toString();
		} catch (err) {
			console.error("AES:2 解密失败", err);
		}
	},

	/**
	 * 使用全局配置进行AES解密
	 * @param {string} words - 需要解密的内容
	 * @returns {string} 解密后的字符串
	 */
	AESDecrypt(words) {
		// 全局状态globalData
		const { globalData } = storeToRefs(useGlobalDataStore())

		const encrypted_pwd_info = globalData.value.ENCRYPTED_PWD_INFO;
		const aes_key = encrypted_pwd_info.AES_KEY;
		const aes_iv = encrypted_pwd_info.AES_IV;
		return this.AES_Decrypt(words, aes_key, aes_iv);
	},


	/**
	 * RSA加密
	 * @param {string} words - 需要加密的内容
	 * @param {string} RSA_PUBLIC_KEY - RSA公钥
	 * @returns {string} 加密后的字符串
	 */
	RSAEncrypt(words, RSA_PUBLIC_KEY = '') {
		const publicKey = RSA_PUBLIC_KEY;
		if (!publicKey) {
			console.error('RSA公钥为空');
			return;
		}
		try {
			let encryptor = new JSEncrypt();
			encryptor.setPublicKey(publicKey);
			return encryptor.encrypt(words);
		} catch (err) {
			console.error("RSA加密失败", err);
		}
	},

	/**
	 * RSA解密
	 * @param {string} words - 需要解密的内容
	 * @param {string} RSA_PRIVATE_KEY - RSA私钥
	 * @returns {string} 解密后的字符串
	 */
	RSADecrypt(words, RSA_PRIVATE_KEY = '') {
		const privateKey = RSA_PRIVATE_KEY;
		if (!privateKey) {
			console.error('RSA私钥为空');
			return;
		}
		try {
			const encryptor = new JSEncrypt();
			encryptor.setPrivateKey(privateKey);
			return encryptor.decrypt(words);
		} catch (err) {
			console.error("RSA解密失败", err);
		}
	},

	/**
	 * MD5加密
	 * @param {string} words - 需要加密的内容
	 * @returns {string} MD5加密后的字符串
	 */
	MD5Encrypt(words) {
		try {
			const word2UTF8 = CryptoJS.enc.Utf8.parse(words);
			const md5Hash = CryptoJS.MD5(word2UTF8);
			return md5Hash.toString(CryptoJS.enc.Hex);
		} catch (err) {
			console.error("MD5加密失败", err);
		}
	},

	/**
	 * Base64编码
	 * @param {string} text - 需要编码的文本
	 * @returns {string} Base64编码后的字符串
	 */
	Base64Encode(text) {
		try {
			return CryptoJS.enc.Base64.stringify(
				CryptoJS.enc.Utf8.parse(text)
			);
		} catch (err) {
			console.error("Base64编码失败", err);
			return null;
		}
	},

	/**
	 * Base64解码
	 * @param {string} encodedText - 需要解码的Base64文本
	 * @returns {string} 解码后的字符串
	 */
	Base64Decode(encodedText) {
		try {
			return CryptoJS.enc.Base64.parse(encodedText).toString(CryptoJS.enc.Utf8);
		} catch (err) {
			console.error("Base64解码失败", err);
			return null;
		}
	},

	// 统一解析数据为JSON数据
	NormJSONData(responseData) {
		return null
	}

};

export default cryptoUtils;